//
//  BN_AboutUsViewController.m
//  BabyNow
//
//  Created by 刘泽琛 on 14-6-11.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//
#define TITLE_FONT 20
#define PART_TITLE_FONT 17
#define CONTENT_FONT 16
static int height = 0;

#import "BN_AboutUsViewController.h"

@interface BN_AboutUsViewController ()

@end

@implementation BN_AboutUsViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    // UI+64
    if (IOS7) {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // UI
    self.title = @"关于";
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"houtui"] style:UIBarButtonItemStylePlain target:self action:@selector(backRootVC)];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    // data
    [self initData];
    for (int i = 0; i<_aboutUsPlist.count; i++) {
        NSString *resultStr = _aboutUsPlist[i];
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:resultStr];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:NSMakeRange(0,resultStr.length)];
        UIFont * tfont = nil;
        switch (i) {
            case 0:
                [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, resultStr.length)];
                tfont = [UIFont systemFontOfSize:12];
                break;
            case 1:
                [str addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"Arial-BoldItalicMT" size:PART_TITLE_FONT] range:NSMakeRange(0, resultStr.length)];
                tfont = [UIFont systemFontOfSize:PART_TITLE_FONT];
                break;
            case 2:
                [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:CONTENT_FONT] range:NSMakeRange(0, resultStr.length)];
                tfont = [UIFont systemFontOfSize:CONTENT_FONT];
                break;
            case 3:
                [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:CONTENT_FONT] range:NSMakeRange(0, resultStr.length)];
                tfont = [UIFont systemFontOfSize:CONTENT_FONT];
                break;
            default:
                break;
        }
        
        UILabel *contentLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        contentLabel.tag = i+1;
        contentLabel.attributedText = str;
        [self.view addSubview:contentLabel];
        
        contentLabel.numberOfLines = 0;
        [contentLabel sizeToFit];
        
        
        if (i == 0) {
            if (MAIN_VERSION > 6) {
                height = 64;
            }else {
                height = 0;
            }
        }else {
            if (i == 1) {
                contentLabel.textAlignment = NSTextAlignmentCenter;
            }
            UILabel *otherLabel = (UILabel *)[self.view viewWithTag:i];
            height = CGRectGetMaxY(otherLabel.frame);
        }
        
        CGSize size = CGSizeMake(300,10000);
        NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];
        CGSize actualsize = [self getTextSize:size disFont:tfont contentStr:_aboutUsPlist[i] contextDic:tdic];
        contentLabel.frame = CGRectMake(15, height+20, 300, actualsize.height);
    }
}

#pragma mark
#pragma mark 文本高度自适应
// 封装自动撑开高度的方法
- (CGSize)getTextSize:(CGSize)provisionsSize disFont:(UIFont *)font contentStr:(NSString *)contentStr contextDic:(NSDictionary *)contextDic {
    
    CGSize size;
    switch (MAIN_VERSION) {
        case 6:
            size = [contentStr sizeWithFont:font constrainedToSize:provisionsSize lineBreakMode:NSLineBreakByWordWrapping];
            break;
        default:
            size = [contentStr boundingRectWithSize:provisionsSize options:NSStringDrawingUsesLineFragmentOrigin |NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine attributes:contextDic context:nil].size;
            break;
    }
    return size;
}

- (void)initData {
    NSString *path = [[NSBundle mainBundle]pathForResource:@"AboutUsPlist" ofType:@"plist"];
    _aboutUsPlist = [[NSArray alloc]initWithContentsOfFile:path];
}

#pragma mark Action

// 返回上级视图
- (void)backRootVC {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)dealloc
{
    NSLog(@"关于我们释放");
}
@end
